﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
//using System.Collections.Generic;
//using System.Collections.ObjectModel;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Yang.DataBase;

public partial class WebServices_getGpsLocations : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/plain";
        Response.Charset = "utf-8";
        string resultJson = "{ \"Machines\":[";
        int nMachines = 0;

        //ArrayList MachineList = new ArrayList();

        string strDate = DateTime.Now.Date.ToString();
        string strSql = "select DeviceCode,Latitude,Longitude,WorkName from GpsInfo where LocationTime > '" + strDate + "' order by DeviceCode";

        try
        {
            DataSet ds = Database.Query(strSql);

            int n = ds.Tables[0].Rows.Count;

            
            if (n > 0)
            {
                nMachines = 1;
                string[] deviceCodes = new string[n];

                for (int i = 0; i < n; i++)
                {
                    deviceCodes[i] = ds.Tables[0].Rows[i][0].ToString();
                    if (i > 0 && deviceCodes[i] != deviceCodes[i - 1])
                        nMachines++;
                }

                AgriMachine[] agriMachines = new AgriMachine[nMachines];
                int j = 0;
                //bool addMachine = true;
                for (int i = 0; i < nMachines; i++)
                {
                    agriMachines[i] = new AgriMachine();
                }
                for (int i = 0; i < n; i++)
                {
                    deviceCodes[i] = ds.Tables[0].Rows[i][0].ToString();

                    if (i > 0 && deviceCodes[i] != deviceCodes[i - 1])
                    {
                            j++;
                            agriMachines[j] = new AgriMachine();
                            agriMachines[j].DeviceCode = deviceCodes[i];
                            agriMachines[j].Lat = double.Parse(ds.Tables[0].Rows[i][1].ToString());
                            agriMachines[j].Lon = double.Parse(ds.Tables[0].Rows[i][2].ToString());
                            agriMachines[j].JobCode = ds.Tables[0].Rows[i][3].ToString();
                    }
                    else
                    {
                        if (j == 0)
                        {
                            agriMachines[j].DeviceCode = deviceCodes[i];
                            agriMachines[j].Lat = double.Parse(ds.Tables[0].Rows[i][1].ToString());
                            agriMachines[j].Lon = double.Parse(ds.Tables[0].Rows[i][2].ToString());
                            agriMachines[j].JobCode = ds.Tables[0].Rows[i][3].ToString();
                        }
                    }

                }

                for (int k = 0; k < nMachines; k++)
                {
                    resultJson += "{\"deviceCode\":\"" + agriMachines[k].DeviceCode + "\",\"lat\":\"" + agriMachines[k].Lat + "\",\"lon\":\"" + agriMachines[k].Lon + "\",\"jobName\":\"" + agriMachines[k].JobCode + "\"},";
                }

                resultJson = resultJson.Substring(0, resultJson.Length - 1);
                resultJson += "]}";
            }
            else
            {
                Log.WriteInfo("没有查找到任何农机定位数据。");
                resultJson = "{ \"Machines\":\"0\"}";
            }
        }
        catch (Exception ex)
        {
            Log.WriteException(DateTime.Now.ToString() + ":" + ex.Message + "\n");
            resultJson = "{ \"Machines\":\"0\"}";
        }


        Response.Write(resultJson);


    }
}


/*
　{ "people": [
　　{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
　　{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},
　　{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
　　]}
*/